﻿namespace Zen.BlackBerry.Web.Controllers
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Web;
	using System.Web.Mvc;
	using Zen.BlackBerry.Web.Models;
	using Zen.BlackBerry.Subscription;
	using Microsoft.Practices.Unity;
	using Microsoft.Practices.ServiceLocation;

	/// <summary>
	/// <c>SubscriptionController</c> implements a MVC controller for actions
	/// related to push application subscriptions.
	/// </summary>
	public class SubscriptionController : Controller
	{
		public SubscriptionController()
		{
		}

		[Dependency]
		public ISubscriptionService SubscriptionService
		{
			get;
			set;
		}

		//
		// GET: /Subscription/pushApplicationId=2993822

		public ActionResult Index(string pushApplicationId)
		{
			return View();
		}

		//
		// GET: /Subscription/Stats

		public ActionResult Stats(string pushApplicationId)
		{
			SubscriberStatisticsModel model = new SubscriberStatisticsModel();

			foreach (var modelCount in SubscriptionService
				.GetDeviceModelCount(pushApplicationId))
			{
				model.DeviceModels.Add(
					new DeviceModelCountModel
					{
						Count = modelCount.Count,
						Model = modelCount.Model
					});
			}
			foreach (var osCount in SubscriptionService
				.GetOsVersionCount(pushApplicationId))
			{
				model.OsVersions.Add(
					new OsVersionCountModel
					{
						Count = osCount.Count,
						OsVersion = osCount.OsVersion
					});
			}

			model.TotalSubscriberCount = SubscriptionService
				.GetSizeByAppId(pushApplicationId);
			model.ActiveSubscriberCount = SubscriptionService
				.GetSizeByAppIdAndStatus(pushApplicationId, SubscriberStatus.Active);
			model.SuspendedSubscriberCount = SubscriptionService
				.GetSizeByAppIdAndStatus(pushApplicationId, SubscriberStatus.Suspended);

			return View(model);
		}
	}
}
